package com.ltsw.mvpbaseproject.baseView;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ComponentActivity;

import com.ltsw.mvpbaseproject.R;
import com.ltsw.mvpbaseproject.basePresenter.BasePresenter;
import com.ltsw.mvpbaseproject.util.MyProgressDialog;

import butterknife.ButterKnife;

/**
 * @author FangHeQiang
 * @description:所有的Activity都继承该类
 * @date :2019/8/28 13:52
 */
public abstract class BaseActivity<T extends BasePresenter> extends AppCompatActivity implements BaseView {
    //网络请求加载框
    private MyProgressDialog mProgressDialogs;
    private int mRequestCount;
    public T mPresenter;
    public BaseActivity<T> mActivity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        mProgressDialogs = new MyProgressDialog(this);
        mActivity = this;
        mPresenter = getPresenter();
        mPresenter.attachView(this, this);
        initView();
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.detachView();
        mPresenter.cancelRequest();
    }


    /**
     * 初始化控件
     */
    public abstract void initView();

    /**
     * 为控件添加点击事件
     */
    public abstract void initData();

    /**
     * 实例化Presenter
     */
    public abstract T getPresenter();

    /**
     * 传递用户创建的Layout  ID值
     */
    protected abstract int getLayoutId();

    @Override
    public void showLoading() {
        if (mProgressDialogs != null && mRequestCount <= 0) {
            mRequestCount = 1;
            mProgressDialogs.showDialog();
        } else {
            ++mRequestCount;
        }
    }

    @Override
    public void hideLoading() {
        if (mRequestCount <= 1) {
            mRequestCount = 0;
            mProgressDialogs.closeDialog();
        } else {
            --mRequestCount;
        }
    }
}
